<?php

namespace app\repositories\eloquent;
use app\repositories\contracts\ClientInterface;
use app\admin\model\Client;
use app\facade\Password;
use think\facade\Cache;
use think\response\Json;

/***
 * Class ClientProvider
 * @package app\repositories\eloquent
 * @Author Vee7
 * @Date 2021/12/14
 * @Time 13:11
 * @Description 实现后台授权应用接口
 */
class ClientProvider implements ClientInterface
{
    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取授权应用分页列表
     */
    public function getClientListPage($page,$limit,$orderby='id',$order='asc'):array
    {
        return Client::limit((($page-1)*$limit),$limit)->order($orderby, $order)->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取授权应用总数据量
     */
    public function getClientListCount():int
    {
        return Client::count();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取授权应用列表
     */
    public function getClientList($orderby='id',$order='asc'):array
    {
        return Client::order($orderby, $order)->select()->toArray();
    }

    /***
     * @param array $id
     * @return bool
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:23
     * @Description 按ID删除授权应用
     */
    public function delClientList(array $id):bool
    {
        $list = Client::select($id);
        $result = $list->delete();
        return $result;
    }

    /***
     * @param array $data
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:43
     * @Description 根据指定参数批量修改授权应用字段值
     *
     */
    public function editClientList(array $data)
    {
        //前端应传递如下格式的二维数组
        //var editData = [{'id':id,'key':value},{...}];
        $Client_obj = new Client;
        foreach ($data as $k=>$v){
            foreach ($v as $key=>$value){
                //清除该应用的缓存
                Cache::delete('client_info_'.$value['client_uname']);
                //如果修改内容包含秘钥，则先加密
                if(array_key_exists('client_scret',$value)){
                    $ClientData = Password::EncryPass($value['client_scret']);
                    $list[] = ['client_id'=>$value['client_id'], 'client_scret'=>$ClientData['password']];
                }else{
                    $list[] = $value;
                }
            }
        }
        //返回修改后的数据
        return $Client_obj->saveAll($list)->toArray();
    }

    /***
     * @param array $addArray
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 14:48
     * @Description 新增授权应用
     */
    public function addClientList(array $addArray)
    {
        $Client_obj = new Client;
        $list = [];
        foreach ($addArray as $v){
            //先查询用户名是否已存在
            $isExist = $Client_obj->where('client_uname',$v['client_uname'])->find();
            if($isExist){
                return false;
            }
            //saveAll函数无法自动添加时间戳，手动添加
            $v['create_time'] = date("Y-m-d H:i:s ");
            $list[] = $v;
        }
        return $Client_obj->saveAll($list)->toArray();
    }

}